package com.ruoyi.web.controller.student_club.admin_controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.student_club.domain.ClubRecommend;
import com.ruoyi.student_club.service.IClubRecommendService;
import com.ruoyi.student_club.vo.app.ClubRecommendVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 首页社团推荐Controller
 * 
 * @author 无尤
 * @date 2022-02-11
 */
@Controller
@RequestMapping("/recommend/recommend")
public class ClubRecommendController extends BaseController {

    private String prefix = "recommend/recommend";

    @Resource
    private IClubRecommendService clubRecommendService;

    @GetMapping()
    public String recommend()
    {
        return prefix + "/recommend";
    }


    @GetMapping("/list")
    @ResponseBody
    public AjaxResult getClubRecommendList(){
        List<ClubRecommendVo> list = clubRecommendService.getClubRecommend();
        return AjaxResult.success(list);
    }

    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(@RequestBody ClubRecommend clubRecommend){
        Long clubId = clubRecommend.getClubId();
        if (clubId == null){
            throw new RuntimeException("请选中要推荐的社团");
        }
        ClubRecommend club = clubRecommendService
                .getOne(new QueryWrapper<ClubRecommend>().eq("club_id", clubId));
        if (club != null){
            throw new RuntimeException("此社团|协会已推荐");
        }
        clubRecommendService.save(clubRecommend);
        return AjaxResult.success();
    }

    /**
     * 删除推荐
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping("/{id:\\d+}")
    public AjaxResult remove(@PathVariable("id") Long id){
        if (id == null){
            throw new RuntimeException("此社团|协会不存在");
        }
        clubRecommendService.removeById(id);
        return AjaxResult.success();
    }
}
